package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Invite;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.InviteService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： InviteController
 *  创建时间：2025/2/20
 */
@RestController
@RequestMapping("/invite")
public class InviteController {

    //邀请表
    @Resource
    private InviteService inviteService;

    //用户表
    @Resource
    private SysUserMapper sysUserMapper;


    //查询团队人数
    @PostMapping("/get/user/invite/count")
    public AjaxResult getUserInviteCount(){
        //返回结果
        return AjaxResult.success(inviteService.count(new LambdaQueryWrapper<Invite>()
                //用户编号
                .eq(Invite::getUserId, SecurityUtils.getUserId())
                //逻辑删除
                .eq(Invite::getDeleteFlag, Boolean.FALSE)
        ));
    }

    //查询团队收益
    @PostMapping("/get/user/team/benefits")
    public AjaxResult getUserTeamBenefits(){
        //查询用户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        //返回结果
        return AjaxResult.success(sysUser);
    }


    //查询 直推 或 间推人数
    @PostMapping("/get/user/invite/records")
    public AjaxResult getUserInviteRecords(@RequestBody Invite invite){
        System.out.println(invite + "我是测试sss");
        //创建分页对象
        Page<Invite> page = new Page<>();
        //设置页码
        page.setCurrent(invite.getCurrent());
        //设置行数
        page.setSize(invite.getSize());
        //执行查询
        inviteService.page(page,new LambdaQueryWrapper<Invite>()
                //用户编号
                .eq(Invite::getUserId, SecurityUtils.getUserId())
                //逻辑删除
                .eq(Invite::getDeleteFlag,Boolean.FALSE)
                //类型
                .eq(Objects.nonNull(invite.getInviteType()),Invite::getInviteType,invite.getInviteType())
                //排序
                .orderByDesc(Invite::getCreateTime)
                //添加分页语句
                .last(" limit " + (invite.getCurrent() - 1) * invite.getSize() +"," + invite.getSize())
        );
        page.setTotal(inviteService.count(new LambdaQueryWrapper<Invite>()
                //用户编号
                .eq(Invite::getUserId, SecurityUtils.getUserId())
                //逻辑删除
                .eq(Invite::getDeleteFlag,Boolean.FALSE)
                //类型
                .eq(Objects.nonNull(invite.getInviteType()),Invite::getInviteType,invite.getInviteType())
        ));
        //遍历page对象
        page.getRecords().stream().forEach(ele->{
            //判断被邀请人id不为 null
            if (Objects.nonNull(ele.getInviteUserId())){
                //查询用户
                SysUser sysUser = sysUserMapper.selectUserById(ele.getInviteUserId());
                //判断查询用户不为 null
                if (Objects.nonNull(sysUser)){
                    if (Objects.nonNull(sysUser.getMemberEndTime()) && sysUser.getMemberEndTime().after(new Date())){
                        sysUser.setMemberName("高级会员");
                    }else{
                        sysUser.setMemberName("普通会员");
                    }
                    //设置用户
                    ele.setInviteUser(sysUser);
                }
            }
        });
        //返回结果
        return AjaxResult.success(page);
    }





}
